<?php
/*
Simple Template Engine
*/
class STE {
    private $vars=array();
    private $mDir='./';
    public function setDir($dir) {
        $this->mDir=$dir;
        if (!is_dir($this->mDir)) {
            throw new Exception('tpl dir:'.$this->mDir.' not exists');
        }
    }
    public function assign($var,$val) {
        $this->vars[$var]=$val;
    }
    public function r($var,$filter=null) {
        if ($filter) {
            $v=$filter(@$this->vars[$var]);
        } else {
            $v=@$this->vars[$var];
        }
        return $v;
    }
    public function e($var,$filter=null) {
        echo $this->r($var,$filter);
    }
    public function render($tpl) {
        foreach (array('','.php','.html','.tpl') as $ext) {
            if (file_exists($this->mDir.'/'.$tpl.$ext)) {
                extract($this->vars);
                include $this->mDir.'/'.$tpl.$ext;
                return true;
            }
        }
        throw new Exception('tpl:'.$tpl.' not exists');
    }
}
